package com.springcloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.springcloud.commons.utils.BeanUtils;
import com.springcloud.commons.utils.PageInfo;
import com.springcloud.mapper.SysUserMapper;
import com.springcloud.mapper.SysUserRoleMapper;
import com.springcloud.model.SysUser;
import com.springcloud.model.SysUserRole;
import com.springcloud.model.vo.UserVo;
import com.springcloud.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * SysUser 表数据服务层接口实现类
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

	 	@Autowired
	    private SysUserMapper userMapper;
	    @Autowired
	    private SysUserRoleMapper userRoleMapper;

	    @Override
	    public List<SysUser> selectByLoginName(UserVo userVo) {
	        SysUser user = new SysUser();
	        user.setLoginName(userVo.getLoginName());
	        EntityWrapper<SysUser> wrapper = new EntityWrapper<SysUser>(user);
	        if (null != userVo.getId()) {
	            wrapper.where("id != {0}", userVo.getId());
	        }
	        return this.selectList(wrapper);
	    }

	    @Override
	    public void insertByVo(UserVo userVo) {
	    	Date date = new Date();
	    	userVo.setCreateTime(date);
	    	SysUser user = BeanUtils.copy(userVo, SysUser.class);
	        this.insert(user);

	        Long id = user.getId();
	        String[] roles = userVo.getRoleIds().split(",");
	        SysUserRole userRole = new SysUserRole();

	        for (String string : roles) {
	            userRole.setUserId(id);
	            userRole.setRoleId(Long.valueOf(string));
	            userRoleMapper.insert(userRole);
//	            int i=1/0;
	        }
	    }

	    @Override
	    public UserVo selectVoById(Long id) {
	        return userMapper.selectUserVoById(id);
	    }

		@Override
		public UserVo selectVoByLoginName(String loginName) {
			return userMapper.selectUserVoByLoginName(loginName);
		}

	    @Override
	    public void updateByVo(UserVo userVo) {
	    	SysUser user = BeanUtils.copy(userVo, SysUser.class);
	        this.updateById(user);

	        Long id = userVo.getId();
	        List<SysUserRole> userRoles = userRoleMapper.selectByUserId(id);
	        if (userRoles != null && !userRoles.isEmpty()) {
	            for (SysUserRole userRole : userRoles) {
	                userRoleMapper.deleteById(userRole.getId());
	            }
	        }

	        String[] roles = userVo.getRoleIds().split(",");
	        SysUserRole userRole = new SysUserRole();
	        for (String string : roles) {
	            userRole.setUserId(id);
	            userRole.setRoleId(Long.valueOf(string));
	            userRoleMapper.insert(userRole);
	        }
	    }

	    @Override
	    public void updatePwdByUserId(Long userId, String md5Hex) {
//	        User user = this.selectById(userId);
	    	SysUser user = new SysUser();
	    	user.setId(userId);
	        user.setPassword(md5Hex);
	        this.updateById(user);
	    }

	    @Override
	    public void selectDataGrid(PageInfo pageInfo) {
	        Page<UserVo> page = new Page<UserVo>(pageInfo.getNowpage(), pageInfo.getSize());
	        List<UserVo> list = userMapper.selectUserVoPage(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal((int)page.getTotal());
	    }

	    @Override
	    public void deleteUserById(Long id) {
	        this.deleteById(id);
	        List<SysUserRole> userRoles = userRoleMapper.selectByUserId(id);
	        if (userRoles != null && !userRoles.isEmpty()) {
	            for (SysUserRole userRole : userRoles) {
	                userRoleMapper.deleteById(userRole.getId());
	            }
	        }
	    }


}